package com.sky.controller.user;

import com.sky.entity.AddressBook;
import com.sky.entity.Category;
import com.sky.result.Result;
import com.sky.service.AddressService;
import com.sky.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/address")
@Api(tags = "用户端-地址管理")
@Slf4j
public class AddressController {

    @Autowired
    private AddressService addressService;

    /**
     * 新增地址
     * @param addressBook
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("新增地址")
    public Result addAddress(@RequestBody AddressBook addressBook) {
        log.info("新增地址");
        addressService.addAddress(addressBook);
        return Result.success();
    }
    /**
     * 查询地址
     *
     */
    @GetMapping("/list")
    @ApiOperation("查询地址")
    public Result<List<AddressBook>> list() {
        log.info("查询地址");
        List<AddressBook> list = addressService.list();
        return Result.success(list);
    }

    /**
     * 删除地址
     * @RequestBody AddressBook addressBook
     * @return
     */
    @DeleteMapping("/delete")
    @ApiOperation("删除地址")
    public Result deleteAdddress(Long id) {
        log.info("删除地址");
        addressService.deleteAddress(id);
        return Result.success();
    }

    /**
     * 设置默认地址
     */
    @PostMapping("/default")
    @ApiOperation("设置默认地址")
    public Result setDefault(Long id) {
        log.info("设置默认地址");
        addressService.setDefault(id);
        return Result.success();
    }

    /**
     * 修改地址
     */
    @PutMapping("/update")
    @ApiOperation("修改地址")
    public Result updateAddress(@RequestBody AddressBook addressBook ) {
        log.info("修改地址");
        addressService.updateAddress(addressBook);
        return Result.success();
    }
    /**
     * 获取地址详情
     * @RequestBody AddressBook addressBook
     */
    @GetMapping("/detail")
    @ApiOperation("获取地址详情")
    public Result<AddressBook> getAddress(Long id) {
        log.info("获取地址详情");
        AddressBook addressBook = addressService.getDetail(id);
        return Result.success(addressBook);
    }
    /**
     * 获取默认地址
     */
    @GetMapping("/default")
    @ApiOperation("获取默认地址")
    public Result<AddressBook> getDefault() {
        AddressBook address = addressService.getDefault();
        return Result.success(address);
    }
}
